<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Rechange
 *
 * @author wupeng
 */
include_once 'ApiAbstract.php';

class Rechange extends ApiAbstract {

    //put your code here

    protected $_tradeNo;
    protected $_paybill;

    public function __construct($trade_no, $paybill, $periodStart, $account) {
        $this->_tradeNo = $trade_no;

        $this->_paybill = $paybill;
        $user = Application_Model_User::load($this->_paybill->AccountId);
        parent::__construct($periodStart, $account);
        $this->_fields = array(
            'transaction_number' => $trade_no,
            'trader' => 'Paypal-adonio-dr',
            'customer_id' => $paybill->AccountId,
            'member' => 'admin',
            'customer_name' => $user->nikename,
            'amount' => $paybill->Money,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $periodStart,
            'period_end' => $account->Money,
            'business_type' => 'czje-dr'
        );
    }

    public function addTransaction() {
        $result = false;
        $fields = $this->getFields();

        if (!empty($fields)) {
            //客户借到paypal,支付宝账户
            $trades = $this->_getTrades();
            if ($this->_paybill->payId == Application_Model_PayBill::PAY_PAYPAL) {
                $this->setFields('rate', $this->_paybill->usmoney)
                        ->setFields('currency','USD')
                        ->setFields('amount', $this->_paybill->usmoney);
            }
            $this->setFields('trader',$trades[$this->_paybill->payId]);
            $this->setFields('period_end',  $this->_periodStart);
            $url = $this->_buildUrl('addTransaction');
            
            $this->_updateMageFinance($url, $this->getFields());
            //贷到客户账户
            $this->setFields('business_type','czje-cr')
                    ->setFields('period_end',$this->_account->Money)
                    ->setFields('trader','backend-account-cr');
            
            $this->_updateMageFinance($url, $this->getFields());
        }

        return $result;
    }


    protected function _getTrades() {
        return array(
            Application_Model_PayBill::PAY_PAYPAL => 'other_Paypal',
            Application_Model_PayBill::PAY_ALIPAY => 'other_alipay',
            Application_Model_PayBill::PAY_CARD => 'xinyongka',
            Application_Model_PayBill::PAY_ACCOUNT => 'backend-account-dr'
        );
        
    }

}
